﻿/*
连接到媒体服务器
并提供NetStream的创建以及管理
提供MIC、Camera的单例创建
*/

class ConnectToMServer{
	// 私有变量
	private var ServerUrl:String ;
	private var ClientId:String;
	private var nc:NetConnection;
	private var out_ns:NetStream;
	private var in_ns:NetStream;
	private var me:Object;
	private var Mic:Microphone;
	private var Cam:Camera;
	// 公有变量
	public static var say:Function;
	public var Connected:Boolean;
	public var NSobjs:Object;
	public var tag:Object;
	// 事件
	public var onConnected:Function;
	public var onRejected:Function;
	public var onClosed:Function;
	public var onError:Function;
	/////////////////////////////////////////////////         构造函数
	public function ConnectToMServer(clientId:String,serverUrl:String){
		if(clientId!=undefined && serverUrl!=undefined && serverUrl!=""){
			ServerUrl=serverUrl;
			ClientId=ClientId;
			initVars();
			Say("初始化完成.");
		}else{
			Say("初始化变量不能为空.");
		}
	}
	private function initVars(){
		NSobjs=new Object();
		NSobjs.me=this;
		Connected=false;
		me=this;
		nc=new NetConnection();
		nc.me=me;
		nc.onStatus=OnNCStatus;
	}
	////////////   =========================  NetConnection
	public function get NC():NetConnection{
		return me.nc;
	}
	///////////// =========================== 连接到选定服务器
	public function connect(){
		var way:Boolean=Boolean(arguments.shift());
		var user:String=String(arguments.shift());
		var psw:String=String(arguments.shift());
		Say("正在连接服务器，方式："+way);
		
		if(way==true){
			if(user==undefined){
				nc.connect(ServerUrl);
			}else if(psw==undefined){
				nc.connect(ServerUrl,user);
			}else if(psw!=undefined){
				nc.connect(ServerUrl,user,psw);
			}
		}else{
			nc.connect(null);
		}
		
	}
	public function close(){
		for(var i in me.NSobjs){
			RemoveNS(i);
		}
		nc.close(); 
		Connected=false;
	}
	////////////=========================== 创建媒体连接
	public function CreateNS(NsName:String):NetStream{
		if(NsName==undefined || NsName==""){
			me.Say("连接关键字不能为空。");
			return null;
		}
		if(!me.Connected) {
			me.Say("未连接状态下无法创建NS。");
			return null;
		}
		if(me.NSobjs[NsName]!=undefined){
			me.Say("已经有名为["+NsName+"]的媒体连接了。");
			return me.NSobjs[NsName];
		}else{
			me.Say("创建名为["+NsName+"]的媒体连接。");
			me.NSobjs[NsName]=new NetStream(me.nc);
			return me.NSobjs[NsName];
		}
	}
	/////////// ========================= 删除媒体连接
	public function RemoveNS(NsName:String):Boolean{
		
		if(NsName==undefined || NsName=="" || typeof(NsName)!="string"){
			me.Say("连接关键字不能为空或是其它类型。");
			return null;
		}
		if(me.NSobjs[NsName]!=undefined){
			me.NSobjs[NsName].close();
			delete me.NSobjs[NsName];
			me.Say("删除了媒体连接["+NsName+"]。");
			return true;
		}else{
			me.Say("未找到名为["+NsName+"]的媒体连接。");
			return false;
		}
	}
	////////////////// =================================   Camera
	public function get CAMERA(){
		return CreateCamera();
	}
	private function CreateCamera():Camera{
		if(me.Cam!=undefined){
			return me.Cam;
		}else{
			me.Cam=Camera.get();
			me.Say("获取系统Camera..."+me.Cam);
			return me.Cam;
		}
	}
	////////////////====================================   MIC
	public function get MIC(){
		return CreateMIC();
	}
	private function CreateMIC():Microphone{
		if(me.Mic!=undefined){
			return me.Mic;
		}else{
			me.Mic=Microphone.get();
			me.Mic.setUseEchoSuppression(true);
			me.Say("获取系统Microphone..."+me.Mic);
			
			return me.Mic;
		}
	}
	//////////////////////////////////////////////////////////  connect 事件
	private function OnNCStatus(info){
		Debug.say=me.Say;
		Debug.traceAllObj(info);
		if(info.code=="NetConnection.Connect.Success"){
			me.Connected=true;
			if(me.onConnected!=undefined){
				me.onConnected(info);
			}
		}
		else if(info.code=="NetConnection.Connect.Rejected"){
			me.Connected=false;
			if(me.onRejected!=undefined){
				me.onRejected(info);
			}
		}
		else if(info.code=="NetConnection.Connect.Closed"){
			me.Connected=false;
			if(me.onClosed!=undefined){
				me.onClosed(info);
			}
		}else{
			me.Connected=false;
			if(me.onError!=undefined){
				me.onError(info);
			}
		}
	}
	//////////////////////////////////////////////
	public static function HELP(){
		
	}
	
	/////////////////////////////////////////////
	private function Say(o){
		say("CTMS:"+o);
	}
}